package controller;

import java.awt.event.MouseEvent;
import java.util.List;

import factory.OccupationFactory;

import main.RunGame;
import occupation.Occupation;
import occupation.OccupationType;
import view.overlay.MenuOverlay;

public class MainMenuController extends Controller{
	
	private MenuOverlay mMainMenu;
	private GameController mGameController;
	private AssignableControlController mACC;
	private MenuOverlay mCreator;
	
	private RunGame mGame;
	
	public MainMenuController(RunGame game, MenuOverlay mainMenu, MenuOverlay creator, AssignableControlController acc, GameController gc) {
		mGame = game;
		mMainMenu = mainMenu;
		mCreator = creator;
		mGameController = gc;
		mACC = acc;
		
		createMainMenu(mMainMenu);
		createNewGameMenu(mCreator);
	}
	
	private void createMainMenu(MenuOverlay overlay) {
		overlay.addMenuOption("New Game", new SimpleMouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				showCreator();
			}
			
		});
		
		overlay.addMenuOption("Controls", new SimpleMouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				mACC.showControls();
				
				
			}
			
		});
	}

	
	private void createNewGameMenu(MenuOverlay overlay) {
		overlay.addMenuOption("Summoner", new SimpleMouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				
				startGame(OccupationType.SUMMONER);
			}
			
			
		});
		overlay.addMenuOption("Sneak", new SimpleMouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				
				startGame(OccupationType.SNEAK);
			}
			
			
		});
		overlay.addMenuOption("Smasher", new SimpleMouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				
				startGame(OccupationType.SMASHER);
			}
			
			
		});
	}
	
	private void startGame(OccupationType oc) {
//		Occupation occ = new Occupation();//OccupationFactory.createOccupation(oc.toString());
		Occupation occ = OccupationFactory.createOccupation(oc.toString());
		mGame.changeOccupation(occ);
		System.out.println("Changed Occupation: " + oc.toString());
		startGame();
	}
	
	private void showCreator() {
		mCreator.setVisible(true);
		mMainMenu.setVisible(false);
	}
	
	public void showMainMenu() {
		if (mMainMenu != null) {
			mMainMenu.setVisible(true);
			mCreator.setVisible(false);
			// tell the game controller to hide the game
			mGameController.hideGame();
		}
	}
	
	public void hideMainMenu() {
		if (mMainMenu != null) {
			mMainMenu.setVisible(false);
			mCreator.setVisible(false);
		}
	}
	
	public void startGame() {
		hideMainMenu();
		mGameController.showGame();
	}

	@Override
	public List<GameControl> getKeyControls() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void reset() {
		// TODO Auto-generated method stub
		
	}

}
